﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class ShoutboxList : BaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            ShowSmiliesLiteral.Text = ShoutboxDal.GetSmilies("DoSmilieInList");
        }

        protected void ShoutBoxListView_DataBound(object sender, EventArgs e)
        {

            var author = (HiddenField)ShoutBoxListView.InsertItem.FindControl("UserNameAuthorShoutBoxHidden");
            var authorLabel = (Label)ShoutBoxListView.InsertItem.FindControl("UserNameAuthorShoutBoxLabel");

            var InsertButtonInsertItem = (Button)ShoutBoxListView.InsertItem.FindControl("InsertButton");
            var CancelButtonInsertItem = (Button)ShoutBoxListView.InsertItem.FindControl("CancelButton");
            var MessageShoutBoxTextBoxInsertItem = (TextBox)ShoutBoxListView.InsertItem.FindControl("MessageShoutBoxTextBox");

            if (Session["AnonymousID"] == null)
            {
                author.Value = ThisMaster.RequesterProfile.UserName;
                authorLabel.Text = ThisMaster.RequesterProfile.UserName;

                InsertButtonInsertItem.Visible = true;
                CancelButtonInsertItem.Visible = true;
                MessageShoutBoxTextBoxInsertItem.Visible = true;
                ShowSmiliesLiteral.Visible = true;
            }
            else
            {
                InsertButtonInsertItem.Visible = false;
                CancelButtonInsertItem.Visible = false;
                MessageShoutBoxTextBoxInsertItem.Visible = false;
                ShowSmiliesLiteral.Visible = false;
            }
        }


        protected void ShoutBoxListView_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            // La textbox apparait sous forme de label ici
            //TextBox MessageShoutBoxTextBox = null;
            //Label MessageShoutBoxLabel = null;

            //var ThisMessage = ShoutBoxListView.Items[e.NewEditIndex];

            //if (ThisMessage != null)
            //    MessageShoutBoxTextBox = (TextBox)ThisMessage.FindControl("MessageShoutBoxTextBox");
            //if (MessageShoutBoxTextBox != null)
            //    MessageShoutBoxTextBox.Text = HttpUtility.HtmlDecode(MessageShoutBoxTextBox.Text);

            //if (ThisMessage != null)
            //    MessageShoutBoxLabel = (Label)ThisMessage.FindControl("MessageShoutBoxLabel");
            //if (MessageShoutBoxLabel != null)
            //    MessageShoutBoxLabel.Text = HttpUtility.HtmlDecode(MessageShoutBoxLabel.Text);
        }

        protected void ShoutBoxListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                FranceTriple.Dal.ShoutBox ThisMessage = null;
                TextBox MessageShoutBoxTextBox = null;

                ThisMessage = (FranceTriple.Dal.ShoutBox)e.Item.DataItem;

                Button btn = e.Item.FindControl("DeleteButton") as Button;
                Button btn2 = e.Item.FindControl("EditButton") as Button;


                if (e.Item != null)
                    MessageShoutBoxTextBox = (TextBox)e.Item.FindControl("MessageShoutBoxTextBox");
                if (MessageShoutBoxTextBox != null)
                    MessageShoutBoxTextBox.Text = MessageShoutBoxTextBox.HtmlToText();

                if (ThisMaster.RequesterProfile != null && ThisMessage.UserNameAuthorShoutBox.ToLower() == ThisMaster.RequesterProfile.UserName.ToLower())
                {
                    if (btn != null)
                        btn.Visible = true;
                    if (btn2 != null)
                        btn2.Visible = true;
                }

                var msg = e.Item.FindControl("MessageShoutBoxLabel") as Label;
                if (msg != null)
                    msg.Text = ShoutboxDal.ReplaceSmiliesTextToImage(msg.Text.TextToHtml());

                var CreationDateShoutBoxLabel = e.Item.FindControl("CreationDateShoutBoxLabel") as Label;
                if (CreationDateShoutBoxLabel != null)
                    CreationDateShoutBoxLabel.Text = ThisMessage.CreationDateShoutBox.ToUserTimeZone(ThisMaster.RequesterTimeZone).ToString();
            }
        }

        protected void ShoutBoxListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
            TextBox MessageShoutBoxTextBox = null;

            if (e.Item != null)
                MessageShoutBoxTextBox = (TextBox)e.Item.FindControl("MessageShoutBoxTextBox");
            if (MessageShoutBoxTextBox != null)
                MessageShoutBoxTextBox.Text = MessageShoutBoxTextBox.TextToHtml();
        }

        protected void ShoutBoxListView_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
            RefreshMasterShoutbox();
        }

        protected void ShoutBoxListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void ShoutBoxListView_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            RefreshMasterShoutbox();
        }

        protected void ShoutBoxListView_ItemDeleted(object sender, ListViewDeletedEventArgs e)
        {
            RefreshMasterShoutbox();
        }

        private void RefreshMasterShoutbox()
        {
            var sb = (ShoutBox)ThisMaster.FindControl("ShoutBox1");
            if (sb != null)
            {
                var up = (UpdatePanel)sb.FindControl("ShoutboxUpdatePanel");
                if (up != null)
                    up.Update();
            }
        }
    }
}